package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.Type;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class CoinCommonService {

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private AliveCoinActivateHistoryService aliveCoinActivateHistoryService;

    @Autowired
    private ShareholderCoinGrantHistoryService shareholderCoinGrantHistoryService;

    @Autowired
    private DealHistoryService dealHistoryService;

    @Autowired
    private ShareholderBuyGrantHistoryService shareholderBuyGrantHistoryService;

    public CoinCompletedInfo createCoinCompletedInfo(Integer coinTypeId) {
        CoinCompletedInfo coinCompletedInfo = new CoinCompletedInfo();

        /** 设置coinType **/
        CoinType coinType = coinTypeService.getByPrimaryKey(coinTypeId);
        coinCompletedInfo.setCoinType(coinType);

        /** 设置coinPrice **/
        //Jedis //jedis = jedisUtil.getJedis();
        BigDecimal coinPrice = new BigDecimal(jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId)));
        coinCompletedInfo.setCoinPrice(coinPrice);

        /** 设置shareholderCoinGrantHistoryList **/
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistoryList = shareholderCoinGrantHistoryService.getsByCoinTypeId(coinTypeId);
        coinCompletedInfo.setShareholderCoinGrantHistoryList(shareholderCoinGrantHistoryList);

        /** 设置grantCount 和 releaseCount **/

        //累计货币配发总量、(已经购买的活币 + 最近7次的配发总量 + 休眠币数量  )  今日货币配发总量   (今日已经购买的活币+今日配发 + 休眠币 )
        BigDecimal totalSleepCoinDividends = new BigDecimal(0);
        BigDecimal totalAliveCoinDividends = new BigDecimal(0);
        BigDecimal totalHaveDivdends = shareholderBuyGrantHistoryService.getTotalHaveDivdends(coinTypeId);
        totalAliveCoinDividends = totalAliveCoinDividends.add(totalHaveDivdends);
        List<ShareholderCoinGrantHistory> shareholderCoinGrantHistories = shareholderCoinGrantHistoryService.getsByCoinTypeIdAndType(coinTypeId, Type.SLEEPCOIN);
        for (ShareholderCoinGrantHistory shareholderCoinGrantHistory : shareholderCoinGrantHistories) {
            BigDecimal count = shareholderCoinGrantHistory.getCount();
            totalSleepCoinDividends = totalSleepCoinDividends.add(count);
        }

        BigDecimal totalSysCount = new BigDecimal(0);
        totalSysCount = totalSysCount.add(totalAliveCoinDividends).add(totalSleepCoinDividends);

        BigDecimal grantCount = totalSysCount;
        BigDecimal releaseCount = totalAliveCoinDividends;
//        for (ShareholderCoinGrantHistory shareholderCoinGrantHistory : shareholderCoinGrantHistoryList) {
//            BigDecimal aliveCoinCount = new BigDecimal(0);
//            BigDecimal sleepCoinCount = new BigDecimal(0);
//            if (shareholderCoinGrantHistory.getType() == 0)
//                aliveCoinCount = shareholderCoinGrantHistory.getCount();
//            if (shareholderCoinGrantHistory.getType() == 1)
//                sleepCoinCount = shareholderCoinGrantHistory.getCount();
//            releaseCount = releaseCount.add(aliveCoinCount);
//            grantCount = grantCount.add(aliveCoinCount.add(sleepCoinCount));
//        }
        coinCompletedInfo.setGrantCount(grantCount);
        coinCompletedInfo.setReleaseCount(releaseCount);


        /** 设置aliveCoinActivateHistoryList 和 aliveCoinActivateCount **/
        List<AliveCoinActivateHistory> aliveCoinActivateHistoryList = aliveCoinActivateHistoryService.getsByCoinType(coinTypeId);
        coinCompletedInfo.setAliveCoinActivateHistoryList(aliveCoinActivateHistoryList);

        BigDecimal aliveCoinActivateCount = new BigDecimal(0);
        for (AliveCoinActivateHistory aliveCoinActivateHistory : aliveCoinActivateHistoryList) {
            aliveCoinActivateCount = aliveCoinActivateCount.add(aliveCoinActivateHistory.getCount());
        }
        coinCompletedInfo.setAliveCoinActivateCount(aliveCoinActivateCount);

        /** 设置userCapitalList 和 frozenCount **/
        List<User> frozenUserList = userService.getAllFrozenUser();
        List<UserCapital> userCapitalList = new ArrayList<>();
        BigDecimal frozenCount = new BigDecimal(0);
        for (User user : frozenUserList) {
            UserCapital userCapital = userCapitalService.getByUserIdAndCoinTypeId(user.getId(), coinTypeId);
            if (userCapital != null) {
                userCapitalList.add(userCapital);
                frozenCount = frozenCount.add(userCapital.getAliveCoinCount());
                frozenCount = frozenCount.add(userCapital.getSleepCoinCount());
            }

        }
        coinCompletedInfo.setUserCapitalList(userCapitalList);
        coinCompletedInfo.setFrozenCount(frozenCount);

        /** 设置 buyBackDealList 和 buyBackCount **/
        List<UserDealHistory> systemBuyBackDealHistoryList = userDealHistoryService.getAllSystemBuyBack(coinTypeId);
        BigDecimal buyBackCount = new BigDecimal(0);
        for (UserDealHistory dealHistory : systemBuyBackDealHistoryList) {
            buyBackCount = buyBackCount.add(dealHistory.getDealAliveCoinCount());
        }
        coinCompletedInfo.setBuyBackDealList(systemBuyBackDealHistoryList);
        coinCompletedInfo.setBuyBackCount(buyBackCount);

        /** 设置 totalDealHistoryList 和 totalDealCount **/
        List<UserDealHistory> totalDealHistoryList = userDealHistoryService.getsByCoinTypeId(coinTypeId);
        String totalDealCountStr = jedisUtil.get(RedisKeyCreator.totalDealCount(coinTypeId));
        BigDecimal totalDealCount = null;
        if (StringUtils.hasText(totalDealCountStr)) {
            totalDealCount = new BigDecimal(totalDealCountStr);
            LocalDateTime updateTime = LocalDateTime.parse(jedisUtil.get(RedisKeyCreator.totalDealCountUpdateTime(coinTypeId)));
            for (UserDealHistory dealHistory : totalDealHistoryList) {
                if (dealHistory.getCreateTime().compareTo(updateTime) > 0) {
                    totalDealCount = totalDealCount.add(dealHistory.getDealAliveCoinCount());
                }
            }
        } else {
            totalDealCount = new BigDecimal(0);
            for (UserDealHistory dealHistory : totalDealHistoryList) {
                totalDealCount = totalDealCount.add(dealHistory.getDealAliveCoinCount());
            }
        }

        coinCompletedInfo.setTotalDealHistoryList(totalDealHistoryList);
        coinCompletedInfo.setTotalDealCount(totalDealCount);

        /** 设置 todayDealHistoryList 和 todayDealCount **/
        List<UserDealHistory> todayDealHistoryList = userDealHistoryService.getsThisDayByCoinTypeId(coinTypeId);
        String todayDealCountStr = jedisUtil.get(RedisKeyCreator.todayDealCount(coinTypeId));
        BigDecimal todayDealCount = null;
        if (StringUtils.hasText(todayDealCountStr)) {
            todayDealCount = new BigDecimal(todayDealCountStr);
            String time = jedisUtil.get(RedisKeyCreator.todayDealCountUpdateTime(coinTypeId));
            System.out.println(time);
            LocalDateTime updateTime = LocalDateTime.parse(time);
            for (UserDealHistory dealHistory : todayDealHistoryList) {
                if (dealHistory.getCreateTime().compareTo(updateTime) > 0) {
                    todayDealCount = todayDealCount.add(dealHistory.getDealAliveCoinCount());
                }
            }
        } else {
            todayDealCount = new BigDecimal(0);
            for (UserDealHistory dealHistory : todayDealHistoryList) {
                todayDealCount = todayDealCount.add(dealHistory.getDealAliveCoinCount());
            }
        }
        coinCompletedInfo.setTodayDealHistoryList(todayDealHistoryList);
        coinCompletedInfo.setTodayDealCount(todayDealCount);
        //jedisUtil.close();
        return coinCompletedInfo;
    }
}
